package com.xiangban.yanglaodajie_admin.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.yanglaodajie_admin.dao.ClassOrActivityOrdersMapper;
import com.xiangban.yanglaodajie_admin.entity.OrdersClassOrActivity;
import com.xiangban.yanglaodajie_admin.entity.dto.OrdersClassOrActivityDto;
import com.xiangban.yanglaodajie_admin.entity.dto.OrdersDto;
import com.xiangban.yanglaodajie_admin.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_admin.exception.BizException;
import com.xiangban.yanglaodajie_admin.utils.CommonUtils;
import com.xiangban.yanglaodajie_admin.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.xiangban.yanglaodajie_admin.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/6/28 17:03
 */
@Service
public class ClassOrActivityOrdersService {

    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ClassOrActivityOrdersMapper classOrActivityOrdersMapper;
    @Autowired
    private PageUtil pageUtil;

    /**
     * 参数：*商家id、订单编号、状态、分类id、用户名、退款状态、查询订单开始和截止时间
     * @param ordersClassOrActivityDto
     * @return 获取课程订单列表
     */
    public PageInfo<OrdersClassOrActivityDto> ClassOrActivityOrders(OrdersClassOrActivityDto ordersClassOrActivityDto) {
        if (commonUtils.getShopId(request) == null){
            throw new BizException(ApiResultCode.FLAG_FAIL_VOUCHER);
        }
        ordersClassOrActivityDto.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        if ( ordersClassOrActivityDto.getValue() != null &&  ordersClassOrActivityDto.getValue().size() >1){
            List<String> value = ordersClassOrActivityDto.getValue();
            for (int i = 0; i < value.size(); i++) {
//                Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(value2.get(0));
//                Date date = new Date(value2.get(0));
                ordersClassOrActivityDto.setDate1(value.get(0));
                ordersClassOrActivityDto.setDate2(value.get(1));
            }
        }
        List<OrdersClassOrActivityDto> list = classOrActivityOrdersMapper.selectOrdersClassOrActivityList(ordersClassOrActivityDto);

        return new PageInfo<>(list);
    }

    /**
     * 参数：*订单编号
     * @param ordersClassOrActivity
     * @return 订单详情
     */
    public Object ClassOrActivityOrderInfo(OrdersClassOrActivity ordersClassOrActivity) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(ordersClassOrActivity.getOrdersNumber())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        ordersClassOrActivity.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        OrdersClassOrActivityDto ordersClassOrActivityDto = classOrActivityOrdersMapper.selectOrdersClassOrActivityInfo(ordersClassOrActivity);
        return ordersClassOrActivityDto;

    }

    /** 参数：课程id，商家id
     * @param ordersClassOrActivity 移动端课程报名信息列表
     * @return
     */
    public PageInfo<OrdersClassOrActivityDto> classOrActivityStudents(OrdersClassOrActivity ordersClassOrActivity) {


        if (BizEmpty(ordersClassOrActivity.getClassOrActivityId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(ordersClassOrActivity.getShopId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }

        List<OrdersClassOrActivityDto> ordersClassOrActivityDtos = classOrActivityOrdersMapper.classStudents(ordersClassOrActivity);

        return new PageInfo<>(ordersClassOrActivityDtos);
    }
}
